package com.laxcen.basiclib.core.task.core.task;

import com.laxcen.basiclib.core.task.ThreadPriority;

/**
 * 任务消息
 */
class TaskMessage {
    static final int WHAT_DEFAULT = 1;
    // 消息任务
    final Runnable command;
    // 延迟时间
    int delayTime;
    // what
    int what = WHAT_DEFAULT;
    // 优先级
    int priority;

    private TaskMessage(Runnable command) {
        this.command = command;
    }

    public static final Builder newBuilder(Runnable command) {
        return new Builder(command);
    }

    public static class Builder {
        private final Runnable command;
        private int delayTime;
        private int what;
        private int priority;

        Builder(Runnable command) {
            this.command = command;
        }

        Builder delayTime(int delayTime) {
            this.delayTime = delayTime;
            return this;
        }

        Builder what(int what) {
            this.what = what;
            return this;
        }

        Builder priority(int priority) {
            if (priority < ThreadPriority.MAX_PRIORITY) {
                priority = ThreadPriority.MAX_PRIORITY;
            }
            if (priority > ThreadPriority.MIN_PRIORITY) {
                priority = ThreadPriority.MIN_PRIORITY;
            }
            this.priority = priority;
            return this;
        }

        public TaskMessage builder() {
            TaskMessage taskMessage = new TaskMessage(command);
            taskMessage.delayTime = delayTime;
            taskMessage.what = what;
            taskMessage.priority = priority;
            return taskMessage;
        }
    }
}
